﻿Public Class frmBarGauge
    Private Enum GaugeAnimation As Integer
        Increase = 0
        Decrease = 1
    End Enum
    Private _AnimateGaugeDirection As GaugeAnimation = GaugeAnimation.Increase

    Private Sub frmBarGauge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BarGauge1.Value = 0
        _AnimateGaugeDirection = GaugeAnimation.Increase
        Timer1.Enabled = False
        Timer1.Interval = 10
        btnStartGauge.Text = "Start Gauge"

        'Set up data sources from FillStyle and FillGradientStyle combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.BarGauge.BackgroundType.FillStyles))
        cboTextLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.BarGauge.TextLabelPositions))
        cboAxisStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.BarGauge.AxisType.AxisStyles))
        cboLabelStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.BarGauge.AxisType.LabelDisplayStyles))
        cboHeatLevelStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.BarGauge.HeatLevel.HeatLevelStyles))

        'Bind Form objects to Gradient Panel Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", BarGauge1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", BarGauge1.Background, "FillStyle")
        pbBackColor1.DataBindings.Add("BackColor", BarGauge1.Background, "BackColor1")
        pbBackColor2.DataBindings.Add("BackColor", BarGauge1.Background, "BackColor2")

        cboTextLabelPosition.DataBindings.Add("SelectedItem", BarGauge1, "TextLabelPosition")
        pbForeColor.DataBindings.Add("BackColor", BarGauge1, "ForeColor")
        pgFont.DataBindings.Add("SelectedObject", BarGauge1, "Font")

        nudRangeStart.DataBindings.Add("Value", BarGauge1.Axis, "Min")
        nudRangeEnd.DataBindings.Add("Value", BarGauge1.Axis, "Max")
        cboAxisStyle.DataBindings.Add("SelectedItem", BarGauge1.Axis, "AxisStyle")
        nudMajorTickCount.DataBindings.Add("Value", BarGauge1.Axis, "MajorTicks")
        nudMinorTickCount.DataBindings.Add("Value", BarGauge1.Axis, "MinorTicks")
        pbTickColor.DataBindings.Add("BackColor", BarGauge1.Axis, "TickColor")
        cboLabelStyle.DataBindings.Add("SelectedItem", BarGauge1.Axis, "LabelDisplayStyle")
        pbLabelColor.DataBindings.Add("BackColor", BarGauge1.Axis, "ForeColor")
        pgLabelFont.DataBindings.Add("SelectedObject", BarGauge1.Axis, "Font")
        nudPaddingTop.DataBindings.Add("Value", BarGauge1.Padding, "Top")
        nudPaddingBottom.DataBindings.Add("Value", BarGauge1.Padding, "Bottom")
        nudPaddingLeft.DataBindings.Add("Value", BarGauge1.Padding, "Left")
        nudPaddingRight.DataBindings.Add("Value", BarGauge1.Padding, "Right")

        cboHeatLevelStyle.DataBindings.Add("SelectedItem", BarGauge1, "HeatLevelStyle")
        nudHeatLevelHeight.DataBindings.Add("Value", BarGauge1, "HeatLevelHeight")

        pbHL1Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel1, "StartColor")
        nudHL1StartValue.DataBindings.Add("Value", BarGauge1.HeatLevel1, "StartValue")
        nudHL1EndValue.DataBindings.Add("Value", BarGauge1.HeatLevel1, "EndValue")
        nudHL1T1Value.DataBindings.Add("Value", BarGauge1.HeatLevel1.Threshold1, "Value")
        pbHL1T1Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel1.Threshold1, "Color")
        nudHL1T2Value.DataBindings.Add("Value", BarGauge1.HeatLevel1.Threshold2, "Value")
        pbHL1T2Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel1.Threshold2, "Color")

        pbHL2Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel2, "StartColor")
        nudHL2StartValue.DataBindings.Add("Value", BarGauge1.HeatLevel2, "StartValue")
        nudHL2EndValue.DataBindings.Add("Value", BarGauge1.HeatLevel2, "EndValue")
        nudHL2T1Value.DataBindings.Add("Value", BarGauge1.HeatLevel2.Threshold1, "Value")
        pbHL2T1Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel2.Threshold1, "Color")
        nudHL2T2Value.DataBindings.Add("Value", BarGauge1.HeatLevel2.Threshold2, "Value")
        pbHL2T2Color.DataBindings.Add("BackColor", BarGauge1.HeatLevel2.Threshold2, "Color")


        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor
        AddHandler pbForeColor.Click, AddressOf ChooseColor
        AddHandler pbTickColor.Click, AddressOf ChooseColor
        AddHandler pbLabelColor.Click, AddressOf ChooseColor

        AddHandler pbHL1Color.Click, AddressOf ChooseColor
        AddHandler pbHL1T1Color.Click, AddressOf ChooseColor
        AddHandler pbHL1T2Color.Click, AddressOf ChooseColor
        AddHandler pbHL2Color.Click, AddressOf ChooseColor
        AddHandler pbHL2T1Color.Click, AddressOf ChooseColor
        AddHandler pbHL2T2Color.Click, AddressOf ChooseColor

        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged

        AddHandler cboTextLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbForeColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pgFont.PropertyValueChanged, AddressOf ControlValueChanged

        AddHandler nudRangeStart.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudRangeEnd.ValueChanged, AddressOf ControlValueChanged
        AddHandler cboAxisStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler nudMajorTickCount.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudMinorTickCount.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbTickColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler cboLabelStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbLabelColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pgLabelFont.PropertyValueChanged, AddressOf ControlValueChanged

        AddHandler nudPaddingTop.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingBottom.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingLeft.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingRight.ValueChanged, AddressOf UpdatePadding

        AddHandler cboHeatLevelStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler nudHeatLevelHeight.ValueChanged, AddressOf UpdatePadding

        AddHandler pbHL1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudHL1StartValue.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudHL1EndValue.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudHL1T1Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbHL1T1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudHL1T2Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbHL1T2Color.BackColorChanged, AddressOf ControlValueChanged

        AddHandler pbHL2Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudHL2StartValue.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudHL2EndValue.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudHL2T1Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbHL2T1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudHL2T2Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbHL2T2Color.BackColorChanged, AddressOf ControlValueChanged

    End Sub

    Private Sub btnStartGauge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGauge.Click
        If Timer1.Enabled Then
            Timer1.Enabled = False
            btnStartGauge.Text = "Start Gauge"
        Else
            Timer1.Enabled = True
            btnStartGauge.Text = "Stop Gauge"
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If BarGauge1.Value >= BarGauge1.Axis.Max AndAlso _AnimateGaugeDirection = GaugeAnimation.Increase Then
            _AnimateGaugeDirection = GaugeAnimation.Decrease
            BarGauge1.Value = BarGauge1.Axis.Max
        ElseIf BarGauge1.Value <= BarGauge1.Axis.Min AndAlso _AnimateGaugeDirection = GaugeAnimation.Decrease Then
            _AnimateGaugeDirection = GaugeAnimation.Increase
            BarGauge1.Value = BarGauge1.Axis.Min
        End If

        Select Case _AnimateGaugeDirection
            Case GaugeAnimation.Increase
                BarGauge1.Value += 1
            Case GaugeAnimation.Decrease
                BarGauge1.Value -= 1
        End Select
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub UpdatePadding()
        BarGauge1.Padding = New Padding(nudPaddingLeft.Value, nudPaddingTop.Value, nudPaddingRight.Value, nudPaddingBottom.Value)
    End Sub
End Class